package cn.com.pxto.dao;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import cn.com.pxto.global.varables.*;
import cn.com.pxto.commons.SearchParam;
import cn.com.pxto.commons.pagesupport.PageInfo;
import cn.com.pxto.dao.pagesupport.hibernate.HibernatePageDaoSupport;
//import cn.com.iactive.model.User;
import cn.com.pxto.model.UserObjectRule;

/**
 * 
 * @author feng jiqiang
 * 
 */
public class UserObjectRuleDAO extends HibernatePageDaoSupport {
	private static Log log = LogFactory.getLog(UserObjectRuleDAO.class);

	/**
	 * add or update the rule
	 * 
	 * @param rule
	 */
	public void saveRule(UserObjectRule rule) {
		log.debug("saveRule");
		getTemplate().saveOrUpdate(rule);
	}
	/**
	 * 
	 * @param rule
	 */
	public void modifyRule(UserObjectRule rule) {
		getTemplate().saveOrUpdate(rule);
	}
	/**
	 * get the single rule by id
	 * 
	 * @param id
	 * @return
	 */
	public UserObjectRule getRule(Integer id) {
		return (UserObjectRule) getTemplate().get(UserObjectRule.class, id);
	}
	/**
	 * list all the rule
	 * @param pageInfo
	 * @return
	 */
	public List getAllRule(PageInfo pageInfo){
	 String HQL="select rule from UserObjectRule as rule where (1=1) order by rule.id" ;
	 return getTemplate().pagedFind(HQL,pageInfo);
	}

	/**
	 * remove the rule by id
	 * 
	 * @param id
	 */
	public void deleteRule(Integer id) {
		Object o = getTemplate().get(UserObjectRule.class, id);
		if (o != null) {
			getTemplate().delete(o);
		}
	}
	/**
	 * ���ҷ�������Ȩ�޿��Ʊ�
	 * @param SearchParams[] ���ֵ����
	 * @return List Ȩ���б�
	 */
	public List listRules(SearchParam[] params){
		StringBuffer HQL = new StringBuffer();
		HQL.append("select rule as UserObjectRule where (1=1)");
		HQL.append(SearchParam.getSimpleWhereSentence(params,"rule"));
		HQL.append(" order by rule.id");
		return getHibernateTemplate().find(HQL.toString(),SearchParam.getSearchValues(params));
	}
	
	/**
	 * �>�src��objectType����UserObjectRule�������Է�ҳ��
	 * @param src
	 * @return
	 */
	public List listRulesBySrc(String src,Integer objectType,Integer srcType){
		String HQL = "select rule from UserObjectRule as rule where rule.src = ? and rule.objectType = ? and rule.srcType = ? order by rule.id";
		Object[] values = {src,objectType,srcType};
		return getHibernateTemplate().find(HQL,values);
	}
	
	/**
	 * �>�src��objectType����UserObjectRule�����Է�ҳ��
	 * @param src
	 * @return
	 */
	public List listRules(String src,Integer objectType,Integer srcType,PageInfo pageInfo){
		String HQL = "select rule from UserObjectRule as rule where rule.src = ? and rule.objectType = ? and rule.srcType = ? order by rule.id";
		Object[] values = {src,objectType,srcType};
		return getTemplate().pagedFind(HQL,values,pageInfo);
	}
	
	/**
	 * �>�src,object��objectType����UserObjectRule
	 * @param src
	 * @return
	 */
	public List listRulesBySrcAndObject(Integer src,Integer objectType,Integer srcType,Integer object){
		String HQL = "select rule from UserObjectRule as rule where rule.src = ? and rule.objectType = ? and rule.srcType = ? and rule.object = ? order by rule.id";
		Object[] values = {src,objectType,srcType,object};
		return getHibernateTemplate().find(HQL,values);
	}
	
	/**
	 * �>�src����UserObjectRule
	 * @param src
	 * @return
	 */
	public List listRulesBySrc(Integer src){
		String HQL = "select rule from UserObjectRule as rule where rule.src = ? order by rule.id";
		return getHibernateTemplate().find(HQL,src);
	}

	/**
	 * �>�src����UserObjectRule
	 * @param src
	 * @return
	 */
	public List listRulesBySrc(String src){
		String HQL = "select rule from UserObjectRule as rule where rule.src = ? order by rule.id";
		return getHibernateTemplate().find(HQL,src);
	}
	public void deleteRulesBySrc(int src){
		String HQL = "from UserObjectRule as rule where rule.src = "+src;
		getHibernateTemplate().delete(HQL);
	}
	
	/**
	 * ����Dic
	 * @param params
	 * @param pageInfo
	 * @return
	 */
	public List listRule(SearchParam[] params){
		StringBuffer HQL = new StringBuffer(
		"select rule from UserObjectRule as rule where (1=1)");
		HQL.append(SearchParam.getSimpleWhereSentence(params,"rule"));
		HQL.append(" order by rule.id");
		return getHibernateTemplate().find(HQL.toString(), SearchParam.getSearchValues(params));
		}

	public List getSrcbyUid(Integer src){
		String HQL = "select u from UserObjectRule as u where u.src=?";
		List list = getTemplate().find(HQL,src);
		return list;
	}


	public void deleteUserRuleForProduct(Integer uid,String sid){
		String HQL = "select r from UserObjectRule as r where (1=1)";
		       HQL += " and r.src=?";
		       HQL += " and r.objectProductSid=?";
		       Object[] para={uid,sid};
		       List list = getTemplate().find(HQL,para);
				if(list.size()>0){
					  for(int i=0;i<list.size();i++){
						  UserObjectRule r = (UserObjectRule)list.get(i);
						  if(r!=null){
							  getTemplate().delete(r);
						  }
					  }
				  }
	}

	

	/**
	 * remove the rule by object
	 * @param uor
	 */
	public void deleteRule(UserObjectRule uor){
		getTemplate().delete(uor);
	}
}
